<?php

/*
 * 提问页控制器
 */
class AskController extends CommonController{
	/*
	 * 载入提问页模板
	 */
	public function index(){
		if(IS_POST){
			if(isset($_SESSION['uid'])){
				
				$reward=$_POST['reward'];
		        $content=$_POST['content'];
				$uid=$_SESSION['uid'];
				$time=time();
				
				if(!$content){
					$this->error('请输入提问内容');
				}
				if($_POST['cid']==0){
					$this->error('请先选择问题分类');
				}
				$cid = (int)$_POST['cid'];
				//提问处理，给ask表插入一条数据，一定要注意cid和uid
			 	$sql=M()->exec("INSERT INTO hd_ask SET content='{$content}',reward={$reward},uid={$uid},cid={$cid},time={$time}");
				$sql1=M()->exec("update hd_user set ask=ask+1 where uid={$uid}");
				$this->success('提问成功',__APP__);
			}else{
				$this->error('请先登陆！');
			}
			
			
			   
		}
		//分配顶级分类
		$this->topCate();
		//累计提问数量
		$this->wenCate();
		
		
		
		
		$this->display();
	}
	
	/**
	 * 获得子集分类
	 */
	public function getSon(){
		$cid = (int)$_POST['id'];
		//获得子集分类
		$sonData = M()->query("SELECT * FROM hd_category WHERE pid={$cid}");
		//以json的形式返回给js
		echo json_encode($sonData);
	}
	
	/*
	 * 问题显示控制器、
	 */
	public function show(){
		//问题库处理(顶级分类)
		$this->topCate();
		//历史回答问题最多的人
		$this->answerCate();
		//后盾问答助人光荣榜
		$this->acceptCate();
		//本日回答问题最多的人
        $this->todayCate();
		//累计提问数量
		$this->wenCate();
		
		//获得GETasid的值，找到对应的问题，然后显示
		$asid=(int)$_GET['asid'];
		$sql="SELECT * FROM hd_ask as a join hd_user as u on a.uid=u.uid WHERE a.asid={$asid}";
		$askCate=M()->query($sql);
//		p($askCate);die;
		$this->assign("askCate",$askCate);
		
		//面包屑导航
		//获得子集cid
		$cid=(int)$_GET['cid'];
		//组合SQ语句返回所有查询结果、
		$data = M()->query("SELECT * FROM hd_category");
		//获得父级分类
		$fuCate = $this->getFather($data, $cid);
		//倒序数组
		$fuCate = array_reverse($fuCate);
	
		$this->assign("fuCate",$fuCate);
		
		//待解决的相关问题
		$sql1= "SELECT * FROM hd_ask where cid={$cid} and asid!={$asid} and solve=0 order by time desc limit 1";
		$xgCate=M()->query($sql1);
		$this->assign("xgCate",$xgCate);
		
		
		if(IS_POST){
			if(isset($_SESSION['uid']) ||isset($_SESSION['username'])){
				
				$content=$_POST['content'];
				$uid=$_SESSION['uid'];
				$username=$_SESSION['username'];
				$time=time();
				
				if(!$content){
					$this->error('请输入提问内容');
				}
				
				$asid = (int)$_GET['asid'];
				//提问处理，给ask表插入一条数据，一定要注意cid和uid
			 	$sql=M()->exec("INSERT INTO hd_answer SET content='{$content}',uid={$uid},asid={$asid},time={$time}");
				$sql1=M()->exec("update hd_ask set answer=answer+1 where asid={$asid}");
				$sql2=M()->exec("update hd_user set answer=answer+1 where uid={$uid}");
				$this->success('回答成功',__APP__);
			}else{
				$this->error('请先登陆！');
			}
			
			
		}
		
		//回答栏
		$anData=M()->query("SELECT * FROM hd_answer as a join hd_user as u on a.uid=u.uid WHERE asid={$asid}");
        foreach($anData as $k=>$v){
		   		$anData[$k]['time'] = date('Y-m-d h:i:s',$anData[$k]['time']);
		}
		$this->assign("anData",$anData);
		//统计回答数量
		$anNum=M()->query("SELECT count(*) as anNum FROM hd_answer WHERE asid={$asid}");
		
		$this->assign("anNum",$anNum);
		
		//载入模板
		$this->display();
	}
	
	private function getFather($data,$cid){
		 static $temp=array();
		 foreach ($data as $v) {
		 	if($v['cid']==$cid){
		 		$temp[] = $v;
				$this->getFather($data,$v['pid']);
		 	}
		 }
		 return $temp;
	}
	
	/**
	 * 采纳
	 */
	public function accept(){
		//获得地址栏的ASID 和ANID
		$uid=$_SESSION['uid'];
		$asid=(int)$_GET['asid'];
		$anid=(int)$_GET['anid'];
		$sql=M()->exec("update hd_answer set accept=1 where anid={$anid}");
		$sql1=M()->exec("update hd_ask set solve=1 where asid={$asid}");
		$sql2=M()->exec("update hd_user set accept=accept+1 where uid={$uid}");
		$this->error('采纳成功！');
		
	}
	
	
}





?>